package ex.com.jm.food.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import ex.com.jm.food.entity.SupType;
import ex.com.jm.food.service.AdminSupTypeService;
import ex.com.jm.food.utils.DateUtils;
import ex.com.jm.food.utils.JsonUtils;

public class AdminSupTypeAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private SupType supType;
	private AdminSupTypeService adminSupTypeService;
	private Integer supId;
	private int initPage=1;
	private int pageRow=10;
	private int totalCount;
	private String supVale;
	public SupType getSupType() {
		return supType;
	}
	public void setSupType(SupType supType) {
		this.supType = supType;
	}
	public AdminSupTypeService getAdminSupTypeService() {
		return adminSupTypeService;
	}
	public void setAdminSupTypeService(AdminSupTypeService adminSupTypeService) {
		this.adminSupTypeService = adminSupTypeService;
	}
	public Integer getSupId() {
		return supId;
	}
	public void setSupId(Integer supId) {
		this.supId = supId;
	}
	public int getInitPage() {
		return initPage;
	}
	public void setInitPage(int initPage) {
		this.initPage = initPage;
	}
	public int getPageRow() {
		return pageRow;
	}
	public void setPageRow(int pageRow) {
		this.pageRow = pageRow;
	}
	public int getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	public String getSupVale() {
		return supVale;
	}
	public void setSupVale(String supVale) {
		this.supVale = supVale;
	}
	
	/**
	 * 到达大类列表页面
	 * @return
	 */
	public String supTypeList(){
		return "supTypeList";
	}
	/**
	 * 获取数据
	 */
	public void getSupTypes(){
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setContentType("text/plain;charset=utf-8");
		int offset=(initPage-1)*pageRow;
		List<SupType> supTypes=this.getAdminSupTypeService().getSupTypes(offset, pageRow, supVale);
		JSONArray jsonsup=JSONArray.fromObject(supTypes);
		totalCount=this.getAdminSupTypeService().supTypeCount(supVale);
		JSONObject jo=JsonUtils.buildJson(jsonsup, initPage, pageRow, totalCount);
		PrintWriter out=null;
		try {
			out=response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		out.print(jo);
		out.flush();
		out.close();
	}
	/**
	 * 获取大类信息
	 * @return
	 */
	public String editSupType(){
		if(supId!=null){
			supType=this.getAdminSupTypeService().editSupType(supId);
		}
		return "edit";
	}
	/**
	 * 保存或者更新大类信息
	 * @return
	 */
	public String saveSupType(){
		if (supType.getSupName()==null||supType.getSupName().equals("")) {
			addFieldError("supType.supName", "大类名称不能为空");
			return "edit";
		}
		SupType checkName=this.getAdminSupTypeService().checkName(supVale);
		if(checkName!=null&&supType.getSupId()==null){
			addFieldError("supType.supName", "大类名称已存在");
			return "edit";
		}
		if(supType.getSupId()==null&&supType.getSupId().equals("")){
			supType.setCreated(DateUtils.getCurrentDate());
		}
		
		if(supType.getSupId()!=null&&!supType.getSupId().equals("")){
			
			this.getAdminSupTypeService().updateSupType(supType);
			return "supTypeList";
		}
		this.getAdminSupTypeService().saveSupType(supType);
		return "supTypeList";
	}
	/**
	 * 检验名称属否重复
	 */
	public void checkName(){
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setContentType("text/javascript");
		SupType checkName=this.getAdminSupTypeService().checkName(supVale);
		String msg="";
		if(checkName!=null&&supId==null){
			msg="大类名称已存在";
		}else {
			msg="大类名称可以添加";
		}
		JSONArray jo=JsonUtils.buildMessage(msg);
		PrintWriter out=null;
		try {
			out=response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		out.print(jo);
		out.flush();
		out.close();
	}
	/**
	 * 删除大类信息、由于级联关系将其下面的小类也删除
	 * @return
	 */
   public String delSupType(){
	   this.getAdminSupTypeService().delSupType(supId);
	   return "supTypeList";
   }
}
